﻿using System;

namespace CommonUtils.Logging.LoggingProviders
{
    /// <summary>
    /// Base logging provider implementation
    /// </summary>
    public abstract class BaseLoggingProvider: ILoggingProvider
    {
        #region Properties

        /// <summary>
        /// Logging level
        /// </summary>
        public MessageType LoggingLevel
        {
            get;
            set;
        }

        /// <summary>
        /// Default logging level
        /// </summary>
        protected static MessageType DefaultLoggingLevel
        {
            get
            {
                return MessageType.Info;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor with logging level specification
        /// </summary>
        protected BaseLoggingProvider(): this(DefaultLoggingLevel)
        {
        }

        /// <summary>
        /// Constructor with logging level specification
        /// </summary>
        /// <param name="loggingLevel">Logging level</param>
        protected BaseLoggingProvider(MessageType loggingLevel)
        {
            LoggingLevel = loggingLevel;
        }

        #endregion

        #region Implementation of ILoggingProvider

        /// <summary>
        /// Log message
        /// </summary>
        /// <param name="dateTime">Time of event to log</param>
        /// <param name="messageType">Message type</param>
        /// <param name="message">Message</param>
        public void LogMessage(DateTime dateTime, MessageType messageType, string message)
        {
            if (messageType >= LoggingLevel)
            {
                LogMessageImpl(dateTime, messageType, message);
            }
        }

        /// <summary>
        /// Initialization of logging provider
        /// </summary>
        public virtual void InitializeLog()
        {
        }

        /// <summary>
        /// Initialization of logging provider
        /// </summary>
        public virtual void FinalizeLog()
        {
        }

        /// <summary>
        /// Provider name
        /// </summary>
        public virtual string Name
        {
            get
            {
                return GetType().Name;
            }
        }

        #endregion

        #region Abstracts

        /// <summary>
        /// Log message
        /// </summary>
        /// <param name="dateTime">Time of event to log</param>
        /// <param name="messageType">Message type</param>
        /// <param name="message">Message</param>
        public abstract void LogMessageImpl(DateTime dateTime, MessageType messageType, string message);

        #endregion
    }
}